<?php
/**
 * 净水器管理 营销管理功能之一
 */
namespace Admin\Controller;
use Think\Controller;
//use Think\Log;
class WaterPurifierController extends CommonController
{
    protected $model;
    protected $return;
    protected function _initialize()
    {
        parent::_initialize();
        header("Access-Control-Allow-Origin: *");
        $this->model = D('WaterPurifier');
        $this->return = array('errcode'=>0, 'errmsg'=>'ok');
    }

    //列表
    public function index()
    {
        if (IS_POST) {
            $brand = I('post.brand', 0, 'intval');
            $introduce = $this->model->getConfig('introduce', $brand);
            //领取
            $receive = $this->model->getConfig('receive', $brand);
            //更换 
            $replace = $this->model->getConfig('replace', $brand);
            //维修
            $repair = $this->model->getConfig('repair', $brand);
            $data = array('introduce'=>$introduce,'receive'=>$receive,
                          'replace'=>$replace, 'repair'=>$repair);
            $this->return['data'] = $data;
            $this->ajaxReturn($this->return, 'JSON');
        }else{
            $brand = I('get.brand', 0, 'intval');
            $this->assign('brand', $brand);
            $this->assign('breadcrumb1', '营销管理');
            $this->assign('breadcrumb2', '净水器');
            $this->assign('breadcrumb3', '设置');
           
            $this->display();
        }
    }


    //新增介绍
    public function addIntroduce()
    {
        $coverPic = I('cover_pic', '', 'trim');
        $pics = I('pics'); 
        $tel = I('tel','', 'trim'); 
        $brand = I('post.brand', 0, 'intval'); 
        $res = $this->model->addIntroduce($coverPic, $pics, $tel, $this->userId, $brand);     
        if ($res) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器介绍', 1, $res);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //新增领取配置
    public function addReceive()
    {
        $data['actually_price'] = I('post.actually_price', 0);
        $data['actually_name']  = I('post.actually_name', '', 'trim');
        $data['activity_price'] = I('post.activity_price', 0);
        $data['activity_day']   = I('post.activity_day', 0, 'intval');
        $data['deliver_price']  = I('post.deliver_price', 0);
        $data['install_price']  = I('post.install_price', 0);
        $data['deliver_name']   = I('post.deliver_name', '', 'trim');
        $data['install_name']   = I('post.install_name', '', 'trim');
        $data['description']    = I('post.description', 'description', 'trim');
        $brand                   = I('post.brand', 0, 'intval');
        $res = $this->model->addReceive($data, $this->userId, $brand);
        if ($res) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器领取配置', 1, $res);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //新增更换配置
    public function addReplace()
    {
        $service['price'] = I('post.service_price', 0);
        $service['name'] = I('post.service_name', '', 'trim');
        $service['suggest_day'] = I('post.suggest_day', 0, 'intval');
        $configs = I('configs');
        $brand = I('post.brand', 0, 'intval');
        $res = $this->model->addReplace($service, $configs, $this->userId, $brand);
        if ($res) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器更换配置', 1, $res);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //新增维修配置
    public function addRepair()
    {
        $repair['price'] = I('post.repair_price', 0);
        $repair['name'] = I('post.repair_name', '', 'trim');
        $configs = I('configs');
        $brand = I('post.brand', 0, 'intval');
        $res = $this->model->addRepair($repair, $configs, $this->userId, $brand);
        if ($res) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器维修配置', 1, $res);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }
    //订单列表
    public function orderList()
    {
        if(IS_GET){
            $isIFrame = I('get.isIFrame', 0);
            if ($isIFrame) {
                $name = I('get.name', '');
                $user_id = I('get.user_id', '');
                $term = I('get.term', '');
                $pay_status = I('get.pay_status', 0);
                $brand = I('get.brand', 0, 'intval');
                $this->assign('name', urldecode($name));
                $this->assign('user_id', $user_id);
                $this->assign('pay_status', $pay_status);
                $this->assign('term', urldecode($term));
                $this->assign('isIFrame', $isIFrame);
                $this->assign('brand', $brand);
                $this->display('orderListIFrame');
            }else{
                $this->assign('breadcrumb1', '营销管理');
                $this->assign('breadcrumb2', '净水器');
                $this->assign('breadcrumb3', '订单管理');
                $this->display();
            }
        }else{
            $data['start'] = I('post.start', '', 'trim');
            $data['end'] = I('post.end', '', 'trim');
            $data['keyword'] = I('post.keyword', '', 'trim');
            $data['order_type'] = I('post.order_type', 0, 'intval');
            $data['day'] = I('post.day', '-1', 'intval');
            $data['province'] = I('post.province', '', 'trim');
            $data['city'] = I('post.city', '', 'trim');
            $data['page'] = I('post.page', 1, 'intval');
            $data['page_size'] = I('post.page_size', 10, 'intval');

            $data['user_id'] = I('post.user_id', 0, 'intval');
            $data['pay_status'] = I('post.pay_status', 0, 'intval');
            $data['brand'] = I('post.brand', 0, 'intval');
            
            $res = $this->model->OrderList($data);

            if ($res) {
                $this->return = array_merge($this->return, $res);
                $this->ajaxReturn($this->return);
            }else{
                $this->ajaxReturn($this->model->getErrMsg());
            }
        }
    }

    //订单详情
    public function orderDetail()
    {
        $orderId = I('post.order_id', 0, 'intval');
        $order = $this->model->getOrderInfo($orderId);

        if ($order) {
            $this->return['data'] = $order;
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //取消订单
    public function orderCancel()
    {
        $data['order_id'] = I('post.order_id', 0, 'intval');
        $data['pay_time'] = I('post.pay_time', '', 'trim');
        $data['remark']  = I('post.remark', '', 'trim');
        $data['voucher'] = I('post.voucher', array()); 

        $res = $this->model->orderCancel($data);

        if ($res) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器订单取消', 3, $data['order_id']);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //订单备注
    public function remark()
    {
        $orderId = I('post.order_id', 0, 'intval');
        $remark = I('post.remark', '', 'trim');
        $res = $this->model->remark($orderId, $remark);
        if($res){
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器订单备注', 3, $orderId);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //收支明细
    public function incomeDetail()
    {
        if (IS_GET) {
            $this->assign('breadcrumb1', '营销管理');
            $this->assign('breadcrumb2', '净水器');
            $this->assign('breadcrumb3', '收支明细');
            $this->display();
        }else{
            $data['keyword'] = I('post.keyword', '', 'trim');
            $data['start'] = I('post.start', '', 'trim');
            $data['end'] = I('post.end', '', 'trim');
            $data['payment'] = I('post.payment', 0, 'intval');
            $data['type'] = I('post.type', 0, 'intval');
            $data['page'] = I('post.page', 1, 'intval');
            $data['page_size'] = I('post.page_size', 10, 'intval');
            $data['brand'] = I('post.brand', 0, 'intval');
            $res = $this->model->getPayRecord($data);
            if($res !== false){
                $this->return = array_merge($this->return, $res);
                $this->ajaxReturn($this->return, 'JSON');
            }else{
                $this->ajaxReturn(array('errcode'=>50000,'errmsg'=>'查找失败'), 'JSON');
            }
        }
    }

    //订单统计
    public function orderStastics()
    {
        if (IS_GET) {
            $this->assign('breadcrumb1', '营销管理');
            $this->assign('breadcrumb2', '净水器');
            $this->assign('breadcrumb3', '订单收款明细');
            $this->display();
        }else{
            $data['start'] = I('post.start', '', 'trim');
            $data['end'] = I('post.end', '', 'trim');
            $data['keyword'] = I('post.keyword', '', 'trim');
            $data['page_size'] = I('post.page_size', 10, 'intval');
            $data['page'] = I('post.page', 1, 'intval');
            $data['brand'] = I('post.brand', 0, 'intval');
            $res = $this->model->getStastics($data);
            if($res !== false){
                $this->return = array_merge($this->return, $res);
                $this->ajaxReturn($this->return, 'JSON');
            }else{
                $this->ajaxReturn(array('errcode'=>50000,'errmsg'=>'查找失败'), 'JSON');
            }
        }
    }

    //订单统计导出
    public function stasticsDownload()
    {
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');
        $data['keyword'] = I('post.keyword', '', 'trim');
        $data['page_size'] = I('post.page_size', 10, 'intval');
        $data['page'] = I('post.page', 1, 'intval');
        $data['brand'] = I('post.brand', 0, 'brand');
        $res = $this->model->getStastics($data, true);
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'客户名称','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'应付金额','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'已付金额','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'已退金额','size'=>'20'),
        );
        $title = '净水器订单收款统计';
        $type = 18;
        phpExcel($sheetindex,$res['data'],$title,$type);
        exit;
    }

    //导出
    public function download()
    {
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');
        $data['keyword'] = I('post.keyword', '', 'trim');
        $data['order_type'] = I('post.order_type', 0, 'intval');
        $data['day'] = I('post.day', -1, 'intval');
        $data['province'] = I('post.province', '', 'trim');
        $data['city'] = I('post.city', '', 'trim');
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('post.page_size', 10, 'intval');
        $data['brand'] = I('post.brand', 0, 'intval');

        $res = $this->model->orderList($data, true);        
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'单号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'下单时间','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'客户名称','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'地址','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'订单类型','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'金额','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'状态','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'付款状态','size'=>'20'),
            'I'=>array('index'=>'I1','name'=>'服务完成时间','size'=>'20'),
        );
        $title = '净水器订单管理';
        $type = 16;
        phpExcel($sheetindex,$res['data'],$title,$type);
        exit;
    }

    //收支导出
    public function incomeDownload()
    {
        $data['keyword'] = I('post.keyword', '', 'trim');
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');
        $data['payment'] = I('post.payment', 0, 'intval');
        $data['type'] = I('post.type', 0, 'intval');
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('post.page_size', 10, 'intval');
        $data['brand'] = I('post.brand', 0, 'intval');
        $res = $this->model->getPayRecord($data, true);

        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'支付流水号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'时间','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'用户名称','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'类型','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'渠道','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'金额','size'=>'20'),
        );
        $title = '净水器收支明细';
        $type = 17;
        phpExcel($sheetindex,$res['data'],$title,$type);
        exit;
    }

    //线下支付
    public function offlinePay()
    {
        $orderId = I('post.order_id', 0, 'intval');

        $res = $this->model->offlinePay($orderId, $this->userId);
        if ($res !== false) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器订单线下收款', 3, $orderId);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //后台确认完成订单
    public function finish()
    {
        $orderId = I('post.order_id', 0, 'intval');

        $res = $this->model->finishOrder($orderId, $this->userId);
        if ($res !== false) {
            //操作日志
            $this->model->operatelog($this->userId, $this->userName, '净水器订单线确认完成', 3, $orderId);
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }

    //删除订单
    public function del()
    {
        $map['user_id'] = I('post.user_id', 0, 'intval');
        $order = M('WaterPurifierOrder');

        $data['is_deleted'] = 1;

        $res = $order->where($map)->save($data);
        if ($res) {
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn(array('err_code'=>50000, 'err_msg'=>'删除失败'));
        }

    }

}